package com.example.smartcity_poverty.ui.AllService.OA.activity

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Html
import android.view.MenuItem
import com.example.smartcity_poverty.App.Companion.url
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.Repository.coroutine
import com.example.smartcity_poverty.logic.ServiceBean.OA.OaDataShowBannerModel
import com.example.smartcity_poverty.logic.util.Util.glide
import com.example.smartcity_poverty.logic.util.Util.show
import com.youth.banner.Banner
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import kotlinx.android.synthetic.main.activity_oa_show_data_list.*
import kotlinx.coroutines.Dispatchers

/**
 * 医院简介
 */
class OaShowDataListActivity : AppCompatActivity() {

    private val id by lazy { intent.getStringExtra("id")?.toInt()}

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_oa_show_data_list)
        title = "医院简介"

        // 填充数据信息
        coroutine(Dispatchers.IO) {
            // 发起网络请求获取数据
            api.getOaDataShow(id!!).apply {
                if (this.code == 200) {
                    // 填充数据
                    runOnUiThread {
                        this.data?.let {
                            oaShowListName.text = it.hospitalName
                            oaShowListContent.text = "简介: ${Html.fromHtml(it.brief)}"
                        }
                    }
                } else runOnUiThread { this.msg?.show() }
            }

        }


        coroutine(Dispatchers.IO) {
            // 获取轮播图
            val resultImage = api.getOaDataShowBanner(id!!)
            // 配置轮播图适配器
            oaShowListBanner.adapter =
                object : BannerImageAdapter<OaDataShowBannerModel.DataBean>(resultImage.data) {
                    override fun onBindView(
                        p0: BannerImageHolder?,
                        p1: OaDataShowBannerModel.DataBean?,
                        p2: Int,
                    p3: Int
                ) {
                    glide(url + p1?.imgUrl, p0?.imageView!!)
                }
            }
        }


        oaShowListOk.setOnClickListener {
            startActivity(Intent(this, OaShowMeActivity::class.java).putExtra("id", id.toString()))
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}